package com.rtmart.imageupload.controller.item;

import com.rtmart.imageupload.Tools.FileTools;
import com.rtmart.imageupload.entity.Item;
import com.rtmart.imageupload.repository.ItemRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.persistence.criteria.Predicate;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/item/")
public class ItemSelectController {

    @Autowired
    private ItemRepository itemRepository;

    @Value("${item.location}")
    private String basePath;

    @GetMapping("pageView/{page}")
    public String viewPage(@PathVariable Integer page, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) {
        String contextPath = httpServletRequest.getContextPath();
        Cookie cookie = new Cookie("page", page.toString());
        cookie.setPath(contextPath + "/item/list");
        //cookie.setMaxAge(86400);
        httpServletResponse.addCookie(cookie);
        return "redirect:/item/list";
    }

    @GetMapping("view/{id}")
    public String editItem(@PathVariable Long id, Model model) {
        model.addAttribute("imageUrlList", FileTools.imageUrlList(new File(basePath + "/" + itemRepository.getOne(id).getNo() + "/")));
        model.addAttribute("item", itemRepository.getOne(id));
        return "item/view";
    }

    @GetMapping("list")
    //@ResponseBody
    public String list(@CookieValue(required = false) Long no,
                       @CookieValue(required = false) String name,
                       @CookieValue(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date lastDate,
                       @CookieValue(required = false) Integer state,
                       @CookieValue(required = false, defaultValue = "1") Integer page,
                       @CookieValue(required = false, defaultValue = "20") Integer pageSize,
                       HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest,
                       Model model) {
        Specification<Item> specification = (root, query, cb) -> {
            List<Predicate> list = new ArrayList<>();
            if (null != no) {
                list.add(cb.equal(root.get("no").as(Long.class), no));
            }
            if (null != name) {
                list.add(cb.like(root.get("name").as(String.class), "%" + name + "%"));
            }
            if (null != lastDate) {
                list.add(cb.equal(root.get("lastDate").as(Date.class), lastDate));
            }
            if (null != state) {
                list.add(cb.equal(root.get("state").as(Integer.class), state));
            }
            Predicate[] p = new Predicate[list.size()];
            return cb.and(list.toArray(p));
        };
        /*
        List<Sort.Order> orders=new ArrayList<>();
        orders.add(new Sort.Order(Sort.Direction.DESC, "date"));
        orders.add(new Sort.Order(Sort.Direction.ASC, "shiftId"));

        Page<Item> itemList = itemRepository.findAll(specification, PageRequest.of(page-1, pageSize));
        */
        Page<Item> itemList = itemRepository.findAll(specification, PageRequest.of(page - 1, pageSize, Sort.by(Sort.Direction.DESC, "lastDate")));
        model.addAttribute("itemList", itemList);
        return "item/list";
    }
}
